<template>
    <div class="login">
        <div class="login-context">
            <!--头部图片-->
            <div class="login-logo">
                <img src="../assets/logo.png" alt="">
            </div>
            <!--form表单-->
            <el-form :model="loginForm" :rules="loginFormRul" ref="loginFormRef" label-width="100px" class="login-box">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" placeholder="账号" tabindex="1"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" v-model="loginForm.password" placeholder="密码" tabindex="2"></el-input>
                </el-form-item>
                <el-form-item class="login-btn">
                    <el-button type="primary" @click="login" @keyup.enter="keyDown">登录</el-button>
                </el-form-item>
                <el-form-item>
                    <div class="box">
                        <div>
                            <el-icon>
                                <Lock />
                            </el-icon>
                            忘记密码？
                        </div>
                        <div>
                            <el-icon>
                                <User />
                            </el-icon>
                            现在注册
                        </div>
                    </div>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import { ElMessage } from 'element-plus'
    import { h } from 'vue'
    import { ElMessageBox } from 'element-plus'
    import jwt from 'jwt-decode';
    export default {
        name: 'VueLogin',

        data() {
            return {
                loginForm: {
                    username: "",
                    password: ""
                },
                loginFormRul: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
                    ]
                },
            };
        },

        create() {

        },

        methods: {
            login() {
                let that = this;
                this.$http.post('user/login', that.loginForm)
                    .then(function (response) {
                        console.log(response);
                        if (response.state == true) {
                            ElMessage.success({
                                message: '登录成功',
                                type: 'success'
                            })
                            var intoken = response.token;
                            window.sessionStorage.setItem("token", intoken);
                            const decoded = jwt(intoken);
                            console.log("jwt解密后的数据：", decoded);
                            if (decoded.utype == "2") {
                                window.sessionStorage.setItem("username", decoded.username)
                                window.sessionStorage.setItem("uauthority", decoded.uauthority)
                                that.$router.push("/index/shop");
                            } else {
                                ElMessageBox.confirm('账号不可用，请联系管理员！');
                            }


                        } else {
                            ElMessage.success({
                                message: '登录失败',
                                type: 'error'
                            })
                        }
                    })
                    .catch(function (error) {
                        console.log(error, "error");
                        ElMessage('服务器内部错误！请联系管理员 1111-1111-1111')
                    })

            },
            keyDown(e) {
                if (e.keyCode == 13) {
                    this.login();
                }
            },

        },
        mounted() {
            window.addEventListener('keydown', this.keyDown);
        },
    };
</script>

<style lang="scss" scoped>
    .login {
        height: 100%;
        background: url(../assets/bg.jpg) no-repeat;
        background-size: cover;
    }

    .login-context {
        width: 500px;
        height: 500px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        border-radius: 10px;
        background-size: contain;
        background-color: #fff;
        box-shadow: 0 0 5px #5B6BE8;
    }

    .login-logo img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: rgb(238, 238, 238);
    }

    .login-box {
        width: 100%;
        position: absolute;
        bottom: 0;
        padding: 0 20px;
        box-sizing: border-box;
    }

    .login-btn {
        width: 200px;

    }

    .el-input {
        width: 100%;
        margin-left: -40px;
        margin-top: 30px;
    }

    .el-button {
        width: 100px;
    }

    .box {
        display: flex;
        flex-direction: row;
        justify-content: space-around;
    }
</style>